package com.w2e.travel.service;

import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class TravelogueViewService {

    private final RedisTemplate redisTemplate;

    private static final String VIEW_COUNT_KEY_PREFIX = "travelogue:views:";

    // 记录游记浏览量
    public void recordTravelogueView(int travelogueId) {
        String viewKey = VIEW_COUNT_KEY_PREFIX + travelogueId;
        redisTemplate.opsForValue().increment(viewKey);
    }

    // 获取游记浏览量
    public Long getTravelogueViews(int travelogueId) {
        String viewKey = VIEW_COUNT_KEY_PREFIX + travelogueId;
        Object views = redisTemplate.opsForValue().get(viewKey);
        return views != null ? Long.parseLong(views.toString()) : 0L;
    }

}
